import  threading
import time

'''
1.了解线程，如何添加线程
'''
# #计算当前激活的线程数、线程名、当前线程
# def main():
#     add_thread = threading.Thread(target=threading_job)
#     add_thread.start()
#     print(threading.active_count())
#     print(threading.enumerate())
#     print(threading.current_thread())
#
# def threading_job():
#     print("当前线程是%s \n" % threading.current_thread())






'''
2.线程的执行和join()方法
'''
# def main():
#     add_thread = threading.Thread(target=threading_job)
#     add_thread.start()
#     add_thread.join() #join表示 ，等该线程执行完成后，再执行后续代码
#     print("任务结束！！")
#
# def threading_job():
#     print("T线程开始！！\n")
#     for i in range(10):
#         time.sleep(0.5)
#     print("T线程结束！！")
# if __name__ == "__main__":
#     main()





'''
3.Queue功能的实现(多批数据分多个线程，将多个线程放入队列以for循环运行) 适用于多组数据（for循环的嵌套）
'''
# from queue import Queue
#
# #这里将处理的单个数据，和Queue对象放入线程函数中
# def counter(numlist,q):
#     for i in range(len(numlist)):
#         numlist[i] = numlist[i]**2
#     #将处理完的结果放入队列数据，方便后续读取,取代return list
#     q.put(numlist)
#
# #队列处理函数
# def  mutithreading_process(datalist):
#     #实例化队列对象和线程列表
#     q = Queue()
#     threading_list = []
#
#     #生成线程 启动线程 加入线程列表
#     for i in range(len(datalist)):
#         add_thraeding = threading.Thread(target=counter,name=i,args=(datalist[i],q))
#         add_thraeding.start()
#         threading_list.append(add_thraeding)
#     #通过join()方法等待所有线程运行完后 再运行之后的代码
#     for  t in threading_list:
#         t.join()
#     # 取得队列中的值
#     results =[]
#     for _ in range(len(datalist)):
#         result = q.get()
#         results.append(result)
#     print(results)
#
#
# if __name__ == "__main__":
#     mutithreading_process(datalist=[[1,2,3],[4,5,6],[7,8,9]])




'''
4.lock锁：多个线程处理同一段数据，如何不相互影响
'''
# def job1():
#     global m ,lock
#     # lock.acquire()
#     for i in range(10):
#         m += 1
#         print(m)
#     # lock.release()
# def job2():
#     global m ,lock
#     # lock.acquire()
#     for i in range(10):
#         m += 10
#         print(m)
#     # lock.release()
#
# if __name__ == "__main__":
#     m = 1
#     # m为全局变量，两个线程一起对其进行操作，难免会使得其内容发生冲突变化
#     #实例化一个锁,为冲突线程所公用
#     lock = threading.Lock()
#     t1 = threading.Thread(target=job1,name="t1")
#     t2 = threading.Thread(target=job2,name="t2")
#     t1.start()
#     t2.start()
#     t1.join()
#     t2.join()

'''
5.线程不一定有效率,i/o锁的存在,实际上只运行了一个线程，只是减少了写入输出的时间，大量数据的话 难以明显提升效率
'''

import threading
import time
from queue import Queue

def job(l, q):
    res = sum(l)
    q.put(res)



def multithreading(l):
    q = Queue()
    threads = []
    for i in range(4):
        t = threading.Thread(target=job, args=(l,q),name='T%i'%i)
        t.start()
        threads.append(t)
    [t.join() for t in threads]
    total = 0
    for _ in range(4):
        total += q.get()
    print(total)

def normal(l):
    total = sum(l)
    print(total)

if __name__ == '__main__':

    #普通做法
    l = list(range(1000000))
    x = l*4
    s_t=time.time()
    normal(x)
    print('normal:',time.time()-s_t)

    #线程做法
    s_t=time.time()
    multithreading(l)
    print('multithreading:',time.time()-s_t)